package com.github.postsanf.yinian.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.activity.BrowseActivity;
import com.github.postsanf.yinian.bean.YNNativeImage;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.nativestick.StickyGridHeadersSimpleAdapter;
import com.github.postsanf.yinian.utils.DisplayUtils;
import com.github.postsanf.yinian.utils.ImageOptHelper;

import java.io.Serializable;
import java.util.List;

/**
 * Created by Administrator on 2016/12/12.
 */

public class PicWallTimeAdapter extends BaseAdapter implements
        StickyGridHeadersSimpleAdapter {

    private Resources mResource;
    private LayoutInflater mInflater;
    private Context mContext;
    private List<YNNativeImage> hasHeaderIdList;
    private boolean dayTime;

    public void setResource(Resources mResource) {
        this.mResource = mResource;
    }

    public PicWallTimeAdapter(Context mContext, List<YNNativeImage> hasHeaderIdList, boolean dayTime) {
        this.mInflater = LayoutInflater.from(mContext);
        this.mContext = mContext;
        this.hasHeaderIdList = hasHeaderIdList;
        this.dayTime = dayTime;
        if (YNSingleton.getInstance().getNativePhoto().size() == 0 || YNSingleton.getInstance().getNativePhoto() != hasHeaderIdList) {
            YNSingleton.getInstance().setNativePhoto(hasHeaderIdList);
        }
    }

    @Override
    public long getHeaderId(int position) {
        return hasHeaderIdList.get(position).getSection();
    }

    @Override
    public int getCount() {
        return hasHeaderIdList.size();
    }

    @Override
    public Object getItem(int position) {
        return hasHeaderIdList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder mViewHolder;
        if (convertView == null) {
            mViewHolder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.yn_pic_wall_item, parent, false);
            mViewHolder.mImageView = (SimpleDraweeView) convertView.findViewById(R.id.native_item_image);
            mViewHolder.mNative_item_select = (ImageView) convertView.findViewById(R.id.native_item_select);
            convertView.setTag(mViewHolder);
        } else {
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        mViewHolder.mNative_item_select.setVisibility(View.GONE);
        String path = "";
        if (hasHeaderIdList.size() > 0) {
            path = hasHeaderIdList.get(position).getThumbnail();
        }
        mViewHolder.mImageView.setTag(path);

        mViewHolder.mImageView.setImageResource(R.drawable.pic_null);

        Uri uri = Uri.parse(path);
        ImageOptHelper.showFrescoNative(uri, mViewHolder.mImageView, mContext);
        final int screenWidth = DisplayUtils.getScreenWidthPixels((Activity) mContext);
        int horizontalSpacing = DisplayUtils.dp2px(mContext, 2);
        // 设置图片宽高
        if (dayTime) {
            int itemWidth = (screenWidth - 2 * horizontalSpacing) / 3;
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
            mViewHolder.mImageView.setLayoutParams(params);
        } else {
            int itemWidth = (screenWidth - 3 * horizontalSpacing) / 4;
            RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(itemWidth, itemWidth);
            mViewHolder.mImageView.setLayoutParams(params);
        }
//        mViewHolder.mImageView.setColorFilter(null);
        // 点击图片大图查看
        mViewHolder.mImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.i("xjs", "miiiiiiii");
                Intent intent = new Intent(mContext, BrowseActivity.class);
                Log.i("xjs", "p11111111111");
                intent.putExtra(CommonConstants.YNPOSITION, position);
                Log.i("xjs", "p22222222");
                intent.putExtra(CommonConstants.YNDATAS, false);
                Log.i("xjs", "p333333333");
                intent.putExtra(CommonConstants.TIMEWALL, true);
                Log.i("xjs", "p44444444");
                intent.putExtra(CommonConstants.YNQNURL, (Serializable) hasHeaderIdList);
                Log.i("xjs", "p55555");
                intent.putExtra("type", "wall");
                Log.i("xjs", "p66666");
                mContext.startActivity(intent);
                Log.i("xjs", "p7777");
            }
        });
        return convertView;
    }

    @Override
    public View getHeaderView(int position, View convertView, ViewGroup parent) {
        final HeaderViewHolder mHeaderHolder;
        if (convertView == null) {
            mHeaderHolder = new HeaderViewHolder();
            convertView = mInflater.inflate(R.layout.yn_pic_wall_header, parent, false);
            mHeaderHolder.mTextView = (TextView) convertView.findViewById(R.id.native_header_time);
            mHeaderHolder.mimageButton = ((ImageView) convertView.findViewById(R.id.all_pic_select));
            convertView.setTag(mHeaderHolder);
        } else {
            mHeaderHolder = (HeaderViewHolder) convertView.getTag();
        }
        if (hasHeaderIdList.size() > 0 && hasHeaderIdList.get(position) != null) {
            mHeaderHolder.mTextView.setText(hasHeaderIdList.get(position).getTime());
        }
        mHeaderHolder.mimageButton.setVisibility(View.GONE);
        return convertView;
    }

    public void setData(List<YNNativeImage> data, boolean isDay) {
        hasHeaderIdList = data;
        dayTime = isDay;
    }

    public class ViewHolder {
        public SimpleDraweeView mImageView;
        public ImageView mNative_item_select;
    }

    public class HeaderViewHolder {
        public TextView mTextView;
        public ImageView mimageButton;
    }
}
